@using System.Collections.Specialized;
@using Orchard.Utility.Extensions;

@using FruitCake.ContactPage.Models
@using FruitCake.ContactPage.ViewModels
           
@{
    ContactPageFormViewModel form = Model.Form;
    ContactPagePart part = Model.Part;
    FormAspect aspect = Model.Layout;

    string nameCaption = string.IsNullOrWhiteSpace(part.NameCaption) ? T("Your name").ToString() : part.NameCaption;
    string mailCaption = string.IsNullOrWhiteSpace(part.MailCaption) ? T("Mail address").ToString() : part.MailCaption;
    string reasonCaption = string.IsNullOrWhiteSpace(part.ReasonCaption) ? T("Reason").ToString() : part.ReasonCaption;
    string subjectCaption = string.IsNullOrWhiteSpace(part.SubjectCaption) ? T("Subject").ToString() : part.SubjectCaption;
    string textCaption = string.IsNullOrWhiteSpace(part.TextCaption) ? T("Message text").ToString() : part.TextCaption;
    
    form.Name = (TempData["ContactPage.Name"] == null ? form.Name : Convert.ToString(TempData["ContactPage.Name"]));
    form.Mail = (TempData["ContactPage.Mail"] == null ? form.Mail : Convert.ToString(TempData["ContactPage.Mail"]));    
    form.ReasonIndex = (TempData["ContactPage.ReasonIndex"] == null ? form.ReasonIndex : Convert.ToString(TempData["ContactPage.ReasonIndex"]));
    form.Subject = (TempData["ContactPage.Subject"] == null ? form.Subject : Convert.ToString(TempData["ContactPage.Subject"]));    
    form.Text = (TempData["ContactPage.Text"] == null ? form.Text : Convert.ToString(TempData["ContactPage.Text"]));

    string urlReferrer = (TempData["ContactPage.UrlReferrer"] == null ? (Context.Request.UrlReferrer == null ? "" : Context.Request.UrlReferrer.AbsoluteUri) : Convert.ToString(TempData["ContactPage.UrlReferrer"]));

    SelectList reasonList = part.IsReasonVisible ? new SelectList(Model.Reasons, "Value", "Text", form.ReasonIndex) : null;
}

@using (Html.BeginForm("Send", "Form", new { area = "FruitCake.ContactPage" }, FormMethod.Post))
{
    @Html.ValidationSummary()
    if (part.IsNameVisible)
    {         
	    <fieldset>
            <label for="Name">@nameCaption</label>
            <input id="Name" class="text @aspect.InputClass" name="Name" type="text" value="@form.Name" maxlength="255" size="@aspect.InputSize"/>
        </fieldset>
    }
    else
    { 
        @Html.Hidden("Name", "-") 
    }
	<fieldset>
        <label for="Mail">@mailCaption</label>
        <input id="Mail" class="text @aspect.InputClass" name="Mail" type="text" value="@form.Mail" maxlength="255" size="@aspect.InputSize"/>
    </fieldset>    
    if (part.IsReasonVisible)
    { 
	    <fieldset>
            <label for="ReasonIndex">@reasonCaption</label>
            @Html.DropDownList("ReasonIndex", reasonList)
        </fieldset>    
    }
    if (part.IsSubjectVisible)
    { 
	    <fieldset>
            <label for="Subject">@subjectCaption</label>
            <input id="Subject" class="text @aspect.InputClass" name="Subject" type="text" value="@form.Subject" maxlength="255" size="@aspect.InputSize"/>
        </fieldset>    
    }
    <fieldset>
        <label for="Text">@textCaption</label>        
        <textarea id="Text" class="text @aspect.TextClass" name="Text" rows="@aspect.TextRows" cols="@aspect.TextCols" maxlength="@part.TextMaxLength">@form.Text</textarea>
        @Html.ValidationMessage("Text")
    </fieldset>    
    <fieldset>
		<input class="button primaryAction" type="submit" value="@T("Submit")" />
        @Html.Link(T("Cancel").ToString(), urlReferrer == Context.Request.Url.AbsoluteUri ? "/" : urlReferrer)                                
        @Html.Hidden("ContentItemId", (int)Model.ContentPart.ContentItem.Id) 
        @Html.Hidden("ReturnUrl", Context.Request.ToUrlString()) 
        @Html.Hidden("UrlReferrer", urlReferrer) 
        @Html.AntiForgeryTokenOrchard() 
	</fieldset>
}